package site.laodengtou.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import site.laodengtou.mapper.TeacherMapper;
import site.laodengtou.pojo.Question;
import site.laodengtou.pojo.QuestionVo;
import site.laodengtou.pojo.Student;
import site.laodengtou.pojo.Teacher;
import site.laodengtou.pojo.Works;
import site.laodengtou.pojo.resultquestion;
import site.laodengtou.service.TeacherService;

@Service
public class TeacherServiceImpl implements TeacherService{

	@Autowired
	TeacherMapper teacherMapper;
	
	
	public void inputInformation(Student stu) {
		
		//先判断学生是否存在
		Student result = teacherMapper.selectRepetitionById(stu);
		if(result==null) {
			teacherMapper.insertStuXinxi(stu);
		}else {
			
		}
	}


	public List<Teacher> tealogin(Teacher tea) {
		
		List<Teacher> list = teacherMapper.tealogin(tea);
		return list;
	}


	public int addItem(Question question) {
		
		return teacherMapper.addItem(question);
	}


	public int releaseTest(Works works) {
		
		return teacherMapper.releaseTest(works);
	}


	public List<Works> selectExamination() {
		//查询所有考次
		List<Works> list = teacherMapper.selectAllWorks();
		
		return list;
	}


	public List<Student> selectStudentById(int worksId) {
		
		List<Student> student = new ArrayList<Student>();
		List<resultquestion> list = teacherMapper.selectStudentByWorksId(worksId);
		//根据ksh查询出考生信息
		if(list.size()>0) {
			for(resultquestion rs:list) {
				Student result = teacherMapper.selectStuByksh(rs.getKsh());
				student.add(result);
			}
		}
		return student;
	}


	public List<QuestionVo> selectStupaper(String ksh, int worksId) {
		//先查询出答卷，然后查询出试卷
		List<QuestionVo> result = new ArrayList<QuestionVo>();
		resultquestion rs = new resultquestion();
		rs.setKsh(ksh);
		rs.setWorkid(worksId);
		List<resultquestion> list = teacherMapper.selectStupaper(rs); //查出了答卷试题
		if(list.size()>0) {
			//查询试题内容
			for(resultquestion rq:list) {
				QuestionVo questionVo = new QuestionVo();
				Question question = teacherMapper.selectQueByqueId(rq.getQuestionid());
				questionVo.setId(question.getId());
				questionVo.setCode(rq.getCode());
				questionVo.setKsh(ksh);
				questionVo.setQuestiontitle(question.getQuestiontitle());
				questionVo.setStatus(rq.isState());
				questionVo.setWorkid(worksId);
				result.add(questionVo);
			}
			
		}
		return result;
	}


	public int changeTheTopic(resultquestion rs) {
		return teacherMapper.changeTheTopic(rs);
	}
}
